home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / environ / environ.c next >
C/C++ Source or Header  |  1988-01-01  |  2KB  |  59 lines

  1. /*    environ.c
  2.  
  3.     This file contains routines to manipulate the system environment
  4.     string on MSDOS.  These routines assume that the assembly routine
  5.     getenv2 has been used to initialize the values of enl and env.
  6.     These routines were compiled with Microsoft's C compiler ver 4.0 
  7.     using the large memory model.
  8.     Compile as : msc environ /AL;
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <dos.h>
  14. #include <string.h>
  15.  
  16. extern char far *env;    /* far pointer to system environment segment */
  17. char far *tem;
  18. extern int enl;        /* system environment's size */
  19.  
  20. char *search_for(str)    /*searches environment for string*/
  21. char    *str;    /*string to search for*/
  22. {
  23.     int    l;    /*scratch string length*/
  24.  
  25.     l=strlen(str);    /*calculate length of argument string*/
  26.     tem=env;
  27.     while(*tem!=0)    /*loop for all strings in environment*/
  28.     {
  29.     if (strnicmp(tem,str,l)==0)    /*if you find it,*/
  30.         return(tem);        /*return a pointer to it*/
  31.     while(*tem++ != 0);        /*else, skip to next string*/
  32.     }
  33.     return(NULL);        /*if no such string, return NULL*/
  34. }
  35.  
  36. del_string(str)        /*deletes a string from the environment*/
  37. char    *str;    /*name (including =) of string to delete*/
  38. {
  39.     if (search_for(str) != NULL)    /*if it exists,then whack it out!*/
  40.         memcpy(tem,tem+strlen(tem)+1,(enl-(int)tem)-strlen(tem)-1);
  41.     return;
  42. }
  43.  
  44. int ins_string(str)    /*insert a new string in the environment*/
  45. char    *str;    /*string (including name=) to add to environment*/
  46. {
  47.     int    l;        /*length of string being inserted*/
  48.  
  49.     l=strlen(str);        /*get length of new string*/
  50.     if ((enl-(int)env)-l <=0)    /*is there room for this?*/
  51.     return(0);        /*no, return zero*/
  52.     tem=env;
  53.     while(*tem != 0)    /*loop until you find the double nul at end*/
  54.     while (*tem++ != 0);    /*skip past end of next string*/
  55.     strcpy(tem,str);        /*copy new string into the environment*/
  56.     *(tem+l+1) = 0;        /*write the second nul at the end*/
  57.     return(1);
  58. }
  59.